/**
 * 
 */
package com.agrec.client;

import pl.rmalinowski.gwt2swf.client.ui.SWFWidget;

import com.agrec.client.listeners.SignInListener;
import com.agrec.client.services.KMRServiceAsync;
import com.agrec.client.services.UserServiceAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
import com.google.gwt.user.client.ui.HasVerticalAlignment.VerticalAlignmentConstant;

/**
 * @author Alaa Gharbawi
 * 
 * KMR Class is the Root Page for all the KMR app. It will contain the
 * Menu and all other pages.
 *
 */
public class KMR implements SignInListener {
	
	/**
	 * I think it is better extends KMRGlobals class to keep track in the whole application
	 * so only this class and under know about the variables not above the general one AGRec
	 */
	
	private VerticalPanel mainPanel;
	private Hyperlink signInLink;
	@SuppressWarnings("unused")
	private UserServiceAsync userService;
	private KMRServiceAsync kmrService;
	private SignInForm form;
	private FlexCellFormatter cellFormatter;
	private KMRDeckPanel kmrDeck;
	
	public KMR(UserServiceAsync userService, KMRServiceAsync kmrService) {
		this.userService = userService;
		this.kmrService = kmrService;
		init();
		try{
			
		}
		catch(Exception e){
			System.out.println(e.getStackTrace());
		}
		
		
	}

	private void init() {
		form = new SignInForm(userService, this);
		
		kmrDeck = new KMRDeckPanel();
		mainPanel = new VerticalPanel();
		
		//banner
		mainPanel.add(banner());
		//Side Flash Animation 2 with HyperLink
		mainPanel.add(content());
		
		mainPanel.add(footer());
		//DeckPanel with MainPage as a start
		//mainPage();
		
		
		//mainPanel.add(signInLink = new Hyperlink("Sign In", true, "signIn"));
		//mainPanel.add(form = new SignInForm(userService, this));
		//form.setVisible(false);
		
		//signInLink.addClickHandler(new ClickHandler(){

			//public void onClick(ClickEvent event) {
			//	form.setVisible(true);
				
			//}});
		
		
		loadGUI(mainPanel);	
		
	}

	

	private Panel footer() {
		HorizontalPanel footer = new HorizontalPanel();
		footer.add(new HTML("&copy 2009 KMR Executive Search"));
		footer.add(new HTML("Web Application by: Alaa Gharbawi"));
		
		
		return footer;
	}

	private Panel content() {
		
		FlexTable content = new FlexTable();
		FlexCellFormatter contentCellFormatter = content.getFlexCellFormatter();
		
		content.setWidget(0,0,leftSidePanel());
		contentCellFormatter.setAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
		
		kmrDeck.add(new AffiliatedCompanies());
		content.setWidget(0,1,kmrDeck);
		contentCellFormatter.setAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
		
		kmrDeck.showWidget("AffiliatedCompanies");
		//The content part goes here
		//content.add(new AffiliatedCompanies());
		
		return content;
	}

	private Panel banner() {
		
		HorizontalPanel mainLinks = new HorizontalPanel();
		
		Hyperlink about = new Hyperlink("About KMR  |", "About");
		about.setStyleName("mainlinks");
		about.addClickHandler(new ClickHandler(){

			//@Override
			public void onClick(ClickEvent event) {
				
				/*** 
				 * Dont need to create a new one every time only first time clicked
				 */
				AboutKMR aboutPage = new AboutKMR();
				kmrDeck.add(aboutPage);
				kmrDeck.showWidget("AboutKMR");
			}
			
		});
		Hyperlink recruitmentServices = new Hyperlink("Recruitment Services  |", "Recruitment Services");
		recruitmentServices.setStyleName("mainlinks");
		Hyperlink opportunities = new Hyperlink("  Opportunities  |", "OpportunitiesPage");
		opportunities.setStyleName("mainlinks");
		opportunities.addClickHandler(new ClickHandler(){

			//@Override
			public void onClick(ClickEvent event) {
				
				/*** 
				 * Dont need to create a new one every time only first time clicked
				 */
				OpportunitiesPage opportunitiesPage = new OpportunitiesPage(kmrService);
				kmrDeck.add(opportunitiesPage);
				kmrDeck.showWidget("OpportunitiesPage");
			}
			
		});
		Hyperlink getHeadHunted = new Hyperlink("  Get HeadHunted  |", "GetHeadhuntedPage");
		getHeadHunted.addClickHandler(new ClickHandler(){

			//@Override
			public void onClick(ClickEvent event) {
				GetHeadhuntedPage getHeadhuntedPage = new GetHeadhuntedPage(kmrService);
				kmrDeck.add(getHeadhuntedPage);
				kmrDeck.showWidget("GetHeadhuntedPage");
			}
			
		});
		getHeadHunted.setStyleName("mainlinks");
		Hyperlink refer = new Hyperlink("  Refer a friend  |", "Refer a friend");
		refer.addClickHandler(new ClickHandler(){

			//@Override
			public void onClick(ClickEvent event) {
				ReferFriendPage referFriendPage = new ReferFriendPage(kmrService);
				kmrDeck.add(referFriendPage);
				kmrDeck.showWidget("ReferFriendPage");
			}
			
		});
		refer.setStyleName("mainlinks");
		Hyperlink cvServices = new Hyperlink("  CV Writing Service  |", "CV Writing Service");
		cvServices.setStyleName("mainlinks");
		Hyperlink contactUs = new Hyperlink("  Contact Us", "Contact Us");
		contactUs.addClickHandler(new ClickHandler(){

			//@Override
			public void onClick(ClickEvent event) {
				BlindedPopup loginPopup = new BlindedPopup("Login",form);
				//loginPopup.setAnimationEnabled(true);
				loginPopup.show();
				//mainPanel.add(signInLink = new Hyperlink("Sign In", true, "signIn"));
				//mainPanel.add();
			}
			
		});
		contactUs.setStyleName("mainlinks");
		
		mainLinks.add(about);
		mainLinks.add(recruitmentServices);
		mainLinks.add(opportunities);
		mainLinks.add(getHeadHunted);
		mainLinks.add(refer);
		mainLinks.add(cvServices);
		mainLinks.add(contactUs);
		
		mainLinks.setSpacing(7);
		mainLinks.setStyleName("mainlinksPanel");
		
		FlexTable table = new FlexTable();
		table.setCellSpacing(6);
    	cellFormatter = table.getFlexCellFormatter();
    	
    	//cellFormatter.setColSpan(2, 0, 2);
    	//cellFormatter.setHorizontalAlignment(2, 0,
    	 //       HasHorizontalAlignment.ALIGN_CENTER);
    	
    	SWFWidget swfWidget = new SWFWidget("images/MaalekIM.swf", 460, 70);
    	
    	table.setWidget(0, 0,new Image("images/logo.jpg"));
    	cellFormatter.setHeight(0, 0, "130px");
    	cellFormatter.setWidth(0, 0, "119px");
    	
    	table.setWidget(0, 1,swfWidget);
    	cellFormatter.setHeight(0, 1, "70px");
    	cellFormatter.setWidth(0, 1, "460px");
    	
    	table.setWidget(0, 2,new HTML("&nbsp;"));
    	cellFormatter.setWidth(0, 2, "119px");
    	
    	
    	VerticalPanel panel = new VerticalPanel();
		panel.add(mainLinks);
		panel.add(table);
		
		return panel;
	}
	
	private Panel leftSidePanel() {
		/*
		 * <ul >
						        <li><a href="approach.php">Impact managers click <u>here</u></a></li>
						        <li><a href="cv.php">Professional CV Writing Service</a></li>
	   				      </ul>
		 */
		VerticalPanel side = new VerticalPanel();
		side.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		side.addStyleName("candidatesmenu");

		SWFWidget swfWidget = new SWFWidget("images/MaalekIM.swf", 200, 250);
		side.setBorderWidth(1);
		side.add(swfWidget);
		
		return side;
		
	}

	private void loadGUI(Widget widget) {
		RootPanel.get("loading").setVisible(false);
        RootPanel.get("container").add(widget);
		
	}

	public void onSignIn(boolean passwordOK, UserToken token) {
		
		if (token != null)
			Window.alert("Welcome: " + token.userName() + " IP:" +token.userIP() +
					" Agent:" +token.userAgent());
		
		
		//change the page as needed when sign in is succesful
		//start KMR.
		
	}
	
	public void onSignOut() {
		// TODO go back to login page when logged out
		
	}

	public void onBrowserEvent(Event event) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(boolean passwordOK) {
		// TODO Auto-generated method stub
		
	}

	public void onError() {
		// TODO Auto-generated method stub
		
	}
}
